//
//  HomepageHeaderView.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/24.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit

class HomepageHeaderView: UIView, SDCycleScrollViewDelegate {

    @IBOutlet weak var bannerView: UIView!
    
    @IBOutlet weak var userView: UIView!
    @IBOutlet weak var avatarImageView: UIImageView!
    
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var applyView: UIView!
    
    @IBOutlet weak var myApplyView: UIView!
    
    @IBOutlet weak var resultView: UIView!
    
    lazy var banner: SDCycleScrollView = {
        let banner = SDCycleScrollView(frame: CGRect(x: 0, y: 0, width: DeviceInfo.screenWidth, height: 200), delegate: self, placeholderImage: R.image.banner())!
        banner.bannerImageViewContentMode = .scaleAspectFill
//        banner.plac
        for subView in banner.subviews {
            if let imageView = subView as? UIImageView {
                imageView.contentMode = .scaleAspectFill
                imageView.clipsToBounds = true
            }
        }
        return banner
    }()
    
    var bannerArray = [BannerModel]()
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        let userTap = UITapGestureRecognizer(target: self, action: #selector(userTapAction))
        userView.addGestureRecognizer(userTap)
        
        let applyTap = UITapGestureRecognizer(target: self, action: #selector(applyTapAction))
        applyView.addGestureRecognizer(applyTap)
        
        let myApplyTap = UITapGestureRecognizer(target: self, action: #selector(myApplyTapAction))
        myApplyView.addGestureRecognizer(myApplyTap)
        
        let reultTap = UITapGestureRecognizer(target: self, action: #selector(reultTapAction))
        resultView.addGestureRecognizer(reultTap)
        
        bannerView.addSubview(banner)
        banner.clickItemOperationBlock = { [weak self] index in
            if let strongSelf = self {
                if strongSelf.bannerArray.count > index {
                    let model = strongSelf.bannerArray[index]
                    if model.url.count > 0, let current = CDUtility.currentViewController() {
                        let vc = WebContentViewController()
                        vc.urlString = model.url
                        current.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            }
        }
    }
    
    @objc func userTapAction() {
        if let info = MyselfSingleton.shared.userInfo {
            UIManager.shared.showQRPage(qrString: info.personNum)
        }
    }
    
    @objc func applyTapAction() {
        
        guard let idString = MyselfSingleton.shared.userInfo?.userID,idString.count > 0 else {
            return
        }
        
        guard let perCardNum = MyselfSingleton.shared.userInfo?.perCardNum,perCardNum.count > 0 else {
            return
        }
        
        let values = ["IDNum": idString,"PerCardNum": perCardNum].convertToJsonString()
        let p0 = "<CodeName>"+"checkinfo"+"</CodeName>"
        let p1 = "<json>"+values+"</json>"
        let paramString = p0 + p1
        
        NetworkingHandle.oldfetchNetworkData(method: "ExeHNService", paramString: paramString, success: { (dict) in
            #if DEBUG
            if let current = CDUtility.currentViewController() {
            let vc = ApplyInfoViewController()
                current.navigationController?.pushViewController(vc, animated: true)
            }
            #else
            if let code = dict["Code"] as? Int,code == 0 {
                
                if let current = CDUtility.currentViewController() {
                    let vc = ApplyInfoViewController()
                    current.navigationController?.pushViewController(vc, animated: true)
                }
                return
            }
            if let code = dict["Code"] as? Int,code == 1 {
                if let msgString = dict["Message"] as? String {
                    CDUtility.showMSG(message: msgString)
                }
            }
            #endif
        }) {
            print("")
        }
    }
    
    @objc func myApplyTapAction() {
        if let current = CDUtility.currentViewController() {
            let vc = MyApplyViewController()
            current.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    @objc func reultTapAction() {
        if let current = CDUtility.currentViewController() {
            let vc = JudgeResultViewController()
            current.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
    @IBAction func orgButtonAction(_ sender: UIButton) {
        
        if let current = CDUtility.currentViewController() {
            let vc = CareOrgViewController()
            current.navigationController?.pushViewController(vc, animated: true)
        }
        
    }
    
    
    @IBAction func msgButtonAction(_ sender: Any) {
        if let current = CDUtility.currentViewController() {
            let vc = MessageViewController()
            current.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    @IBAction func careButtonAction(_ sender: UIButton) {
        if let current = CDUtility.currentViewController() {
            let vc = HomeCareViewController()
            current.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
}
